package com.aia.prm.web.ria.app.ui.qnce;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

import org.apache.log4j.Logger;

import com.aia.prm.BeanFactory;
import com.aia.prm.domain.Qnce;
import com.aia.prm.domain.QnceExample;
import com.aia.prm.domain.QnceExample.Criteria;
import com.aia.prm.domain.VOPlan;
import com.aia.prm.persistence.dao.QnceDAO;
import com.aia.prm.persistence.dao.VOPlanDAO;
import com.aia.prm.service.DefaultService;
import com.aia.prm.service.ServiceResult;
import com.aia.prm.web.ria.app.PrmApplication;
import com.aia.prm.web.ria.app.ui.AlertDialog;
import com.aia.prm.web.ria.app.ui.ConfirmDialog;
import com.cfh.util.Tools;
import com.vaadin.data.Item;
import com.vaadin.data.Property;
import com.vaadin.data.Property.ValueChangeEvent;
import com.vaadin.data.util.BeanItem;
import com.vaadin.terminal.ExternalResource;
import com.vaadin.terminal.Sizeable;
import com.vaadin.terminal.ThemeResource;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.ComboBox;
import com.vaadin.ui.Form;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.HorizontalSplitPanel;
import com.vaadin.ui.Label;
import com.vaadin.ui.Link;
import com.vaadin.ui.PopupView;
import com.vaadin.ui.TextField;
import com.vaadin.ui.VerticalLayout;
import com.vaadin.ui.VerticalSplitPanel;
import com.vaadin.ui.Window.Notification;

@SuppressWarnings("serial")
public class QnceView extends VerticalLayout implements Property.ValueChangeListener {
	private static final Logger logger = Logger.getLogger(QnceView.class);
	private PrmApplication app = null;
	VerticalSplitPanel vert = null;
	HorizontalSplitPanel horiz = null;
	VerticalLayout rightLayout = null;
	QnceList QnceList = null;
	Form form = null;
	QnceContainer QnceContainer = null;
	QnceForm QnceForm = null;
	private QnceDAO QnceDAO = null;
	List<Qnce> list = null;

	VerticalLayout topLayout = null;
//	Button newButton = null;
//	Button xmlButton = null;
	Button okButton = null;

	TextField startDate = new TextField();
	TextField endDate = new TextField();

	private ComboBox plansBox = null;
//	private ComboBox fromBox = null;
//	private ComboBox statusBox = null;
	TextField nameField = new TextField();

	TextField mobileField = new TextField();
	DefaultService defaultService = null;

	Link downloadReportLink = null;

	public QnceView(PrmApplication aapp) {
		app = aapp;
		setQnceDAO((QnceDAO) BeanFactory.get().getBean("qnceDAO"));
		init();
		buildMainLayout();
		load();
	}

	private void init() {
		 downloadReportLink = new Link("下载报表", new ExternalResource(app.getFullContextPath() + "/reportQnce/"));
			{
				downloadReportLink.setTargetName("_blank");
				downloadReportLink.setTargetBorder(Link.TARGET_BORDER_NONE);
				downloadReportLink.setDescription("请选择合适的查询条件，下载过多的记录会可能会给系统造成压力");
				addComponent(downloadReportLink);
			}
		
		defaultService = (DefaultService) BeanFactory.get().getBean("defaultService");
		startDate.setInputPrompt("yyyymmdd");
		endDate.setInputPrompt("yyyymmdd");
		startDate.setValue(Tools.getToday());
		endDate.setValue(Tools.getToday());

		startDate.setWidth(80F, Sizeable.UNITS_PIXELS);
		endDate.setWidth(80F, Sizeable.UNITS_PIXELS);

		nameField.setWidth(60F, Sizeable.UNITS_PIXELS);

		plansBox = new ComboBox();
		VOPlanDAO vOPlanDAO = (VOPlanDAO) BeanFactory.get().getBean("vOPlanDAO");
		List list = vOPlanDAO.selectByExample(null);
		String firstItem = null;
		for (Object o : list) {
			VOPlan p = (VOPlan) o;
			if (firstItem == null) {
				firstItem = p.getPL_ID();
			}
			plansBox.addItem(p.getPL_ID());
			plansBox.setItemCaption(p.getPL_ID(), "[" + p.getPL_ID() + "]" + p.getPL_NAME());
		}
		if (firstItem != null) {
			plansBox.select(firstItem);
		}
	}

	private List makeListBySearch() {
		QnceExample example = new QnceExample();
		Criteria crit = example.createCriteria();

		crit.andQNXE_DATEGreaterThanOrEqualTo(String.valueOf(startDate.getValue())).andQNXE_DATELessThanOrEqualTo(
				String.valueOf(endDate.getValue()));

		String name = (String) nameField.getValue();
		if (Tools.hasText(name)) {
			crit.andQNXE_NAMELike(name + "%");
		}

		String mobile = (String) mobileField.getValue();
		if (Tools.hasText(mobile)) {
			crit.andQNXE_PHOLike(mobile + "%");
		}
		

		List list = getQnceDAO().selectByExample(example);
		return list;
	}

	void load() {
		list = makeListBySearch();
		int size = list == null ? 0 : list.size();
		if (size == 0) {
			app.getMainWindow().showNotification("没有记录");
		} else {
			app.getMainWindow().showNotification("查询记录条数:" + size);
		}

		if (QnceContainer == null) {
			try {
				QnceContainer = new QnceContainer();
			} catch (Throwable e) {
				logger.error(e);
				throw new RuntimeException(e);
			}
		}

		QnceContainer.removeAllItems();
		QnceContainer.addAll(list);

		QnceList.setContainerDataSource(QnceContainer);

		QnceList.setVisibleColumns(QnceContainer.NATURAL_COL_ORDER);
		QnceList.setColumnHeaders(QnceContainer.COL_HEADERS_SHOW);

		QnceForm.setItemDataSource(null);
	}

	private void buildMainLayout() {
		setSizeFull();
		vert = new VerticalSplitPanel();
		vert.setSizeFull();
		vert.setSplitPosition(100, Sizeable.UNITS_PIXELS);
		addComponent(vert);

		// add a label to the upper area
		topLayout = new VerticalLayout();
		topLayout.setSpacing(true);
		topLayout.setMargin(true);
		
		

		// topLayout.addComponent(newButton);
		// topLayout.addComponent(plansBox);
		HorizontalLayout topLayoutSub = new HorizontalLayout();
		HorizontalLayout topLayoutSub2 = new HorizontalLayout();
		topLayoutSub.setSizeUndefined();
		topLayoutSub.setMargin(false);
		topLayoutSub.setSpacing(true);
		topLayoutSub2.setSizeUndefined();
		topLayoutSub2.setMargin(false);
		topLayoutSub2.setSpacing(true);
		topLayoutSub.addComponent(new Label("起始日期:"));
		topLayoutSub.addComponent(startDate);
		topLayoutSub.addComponent(new Label("结束日期:"));
		topLayoutSub.addComponent(endDate);
		topLayoutSub.addComponent(new Label("姓名:"));
		topLayoutSub.addComponent(nameField);
		topLayoutSub.addComponent(new Label("手机号:"));
		topLayoutSub.addComponent(mobileField);

		okButton = new Button("查找记录", new Button.ClickListener() {
			public void buttonClick(ClickEvent event) {
				load();
			}
		});

		okButton.setIcon(new ThemeResource("icons/search.png"));
		topLayoutSub2.addComponent(okButton);
		topLayoutSub2.addComponent(downloadReportLink);


		topLayout.addComponent(topLayoutSub);
		topLayout.addComponent(topLayoutSub2);
		vert.setFirstComponent(topLayout);

		horiz = new HorizontalSplitPanel();
		horiz.setSplitPosition(66); // percent
		vert.setSecondComponent(horiz);

		// left component:
		QnceList = new QnceList(app, this);
		horiz.setFirstComponent(QnceList);

		rightLayout = new VerticalLayout();
		rightLayout.setSizeUndefined();
		rightLayout.setWidth("100%");
		rightLayout.setMargin(true);
		QnceForm = new QnceForm(this);

		rightLayout.addComponent(QnceForm);
		// right component:
		horiz.setSecondComponent(rightLayout);

	}

	public void valueChange(ValueChangeEvent event) {
		if (QnceList.isMultiSelect()) {
			Set<?> value = (Set<?>) event.getProperty().getValue();
			if (null == value || value.size() == 0) {
				return;
			} else {
				Item item = QnceList.getItem(value.iterator().next());
				QnceForm.setItemDataSource(item);
			}
		} else {
			Item item = QnceList.getItem(QnceList.getValue());
			QnceForm.setItemDataSource(item);
		}
	}

	public QnceDAO getQnceDAO() {
		return QnceDAO;
	}

	public void setQnceDAO(QnceDAO QnceDAO) {
		this.QnceDAO = QnceDAO;
	}

	public List fetchReportList() {
		return makeListBySearch();
	}

}
